package com.wy.business.market.home.actions;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.market.home.services.AppsManageService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.constants.RtnCodeConstant;
import com.wy.framework.dao.ErrorSqlException;

/**
 * 项目名称：openportal
 * 类名称：BannerAction
 * 类描述：滚动轮播(banner)
 * 创建人：张旭星
 * 创建时间：2015-12-28 下午7:35:56
 * 修改人：张旭星
 * 修改时间：2015-12-28 下午7:35:56
 * 版本信息：@version 1.0
 * Copyright (c) 2015万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/home/apps")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class AppsAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(AppsAction.class);

	@Autowired
	private AppsManageService appsManageService;
	
	private List<Map> rows;
	private List<Map> orgInfo;
	private String id;

	/**
	 * Function Name               update                                   
	 * @return          		             
	 * @description 移动应用位置
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-7 上午9:30:37           田松林/张旭星                   Initial
	 **********************************************************************
	 */
	 
	@Action(value = "move", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String moveApp() {
		try {
			String userId=ServletActionContext.getRequest().getParameter("userId");
			String functionId=ServletActionContext.getRequest().getParameter("functionId");
			String sort=ServletActionContext.getRequest().getParameter("sort");
			appsManageService.updateUserAppsSort(userId, functionId, Integer.valueOf(sort));
		} catch (NumberFormatException e) {
			rspCode=RtnCodeConstant.RTN_CODE_APP_POSITIONW_RONG;
			e.printStackTrace();
		} catch (ErrorSqlException e) {
			rspCode="999998";
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * Function Name               delete                                   
	 * @return          		             
	 * @description 用户删除一个应用
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-7 上午10:37:11           田松林                   Initial
	 **********************************************************************
	 */
	 
	@Action(value = "delete", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String deleteApp(){
		try {
			String userId=ServletActionContext.getRequest().getParameter("userId");
			String functionId=ServletActionContext.getRequest().getParameter("functionId");
			appsManageService.deleteUserApp(functionId,userId);
		} catch (ErrorSqlException e) {
			rspCode="999999";
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * Function Name               addApp                                   
	 * @return          		             
	 * @description 用户新增一个应用
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-7 上午10:37:11           张旭星                   Initial
	 **********************************************************************
	 */
	
	@Action(value = "addApp", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String addApp(){
		try {
			String userId=ServletActionContext.getRequest().getParameter("userId");
			String functionId=ServletActionContext.getRequest().getParameter("functionId");
			appsManageService.addApp(functionId,userId);
		} catch (ErrorSqlException e) {
			rspCode="999999";
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * Function Name               appListSync                                   
	 * @return          		             
	 * @description 登录时,如果该用户从未记录过,则上传为9,如果该用户登录记录过,则下载之前的列表(sync接口,app传应用列表,如果响应了应用列表,则记录过,响应为空,则上传成功)
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-7 上午10:37:11          张旭星                   Initial
	 **********************************************************************
	 */
	
	@Action(value = "appListSync", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String appListSync(){
		try {
			String userId = ServletActionContext.getRequest().getParameter("userId");
			String functionIds = ServletActionContext.getRequest().getParameter("functionIds");
			rows = appsManageService.updateAppListSync(userId, functionIds);
		} catch (Exception e) {
			rspCode="999999";
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * Function Name               openApp                                   
	 * @return          		             
	 * @description 增加应用访问次数
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-7 上午10:46:02           田松林                   Initial
	 **********************************************************************
	 */
	@Action(value = "open", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String openApp(){
		try {
			String functionId=ServletActionContext.getRequest().getParameter("functionId");
			appsManageService.updateFunctionViews(functionId);
		} catch (ErrorSqlException e) {
			rspCode="999998";
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public List<Map> getRows() {
		return rows;
	}

	public List<Map> getOrgInfo() {
		return orgInfo;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}

}
